<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>vue3自定义表单组件</title>
  </head>
  <body>
    <div id="app">
      <custom-input
        show-clear
        placeholder="用户名"
        v-model="userName"
      ></custom-input>
      {{ userName }}
      <custom-input
        label="密码"
        placeholder="密码"
        type="password"
        v-model="password"
      ></custom-input>
      {{ password }}
    </div>
  </body>
  <template id="input">
    <div>
      <label>{{label}}</label>
      <input
        :type="type"
        :value="modelValue"
        :placeholder="placeholder"
        @input="$emit('update:modelValue', $event.target.value)"
      />
      <span
        v-if="modelValue !=='' && showClear"
        @click="$emit('update:modelValue', '')"
        >❌</span
      >
    </div>
  </template>
  <script src="../lib/vue.global.js"></script>
  <script>
    const Input = {
      template: "#input",
      // props: ['modelValue', 'type'], // 父组件使用 v-model,子组件接受 默认的参数为 modelValue ，将其可以赋值给input的value属性
      props: {
        modelValue: String | Number, // 这里可以接收 string 类型或者 number类型的数据
        type: {
          type: String,
          default: "text",
        },
        placeholder: {
          type: String,
          defualt: "请输入关键词",
        },
        label: String,
        showClear: Boolean,
      },
    };

    Vue.createApp({
      data() {
        return {
          userName: "",
          password: "",
        };
      },
      components: {
        CustomInput: Input,
      },
    }).mount("#app");
  </script>
</html>
